package matlap.client.view.distributor;

import java.util.Iterator;
import java.util.List;

import matlap.client.services.order.OrderService;
import matlap.client.services.order.OrderServiceAsync;
import matlap.shared.Order;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.VerticalPanel;

public class MyOrdersComposite extends Composite {

	OrderServiceAsync c1 = GWT.create(OrderService.class);
	private VerticalPanel vp;
	private Grid g;
	protected String name;
	Panel simplePanel = new VerticalPanel();
	Panel newPanel = new HorizontalPanel();
	Panel gridPanel = new VerticalPanel();

	public MyOrdersComposite(final String un) {
		init(un);
		initGrid(un);
	}

	public void init(final String un) {
		vp = new VerticalPanel();
		initWidget(vp);
		vp.setSize("670px", "108px");

		DecoratorPanel decoratorPanel2 = new DecoratorPanel();
		vp.add(decoratorPanel2);
		Label lblNewLabel_3 = new Label("Eddigi rendel\u00E9seim:");
		lblNewLabel_3.setStyleName("BigBold");
		decoratorPanel2.setWidget(lblNewLabel_3);

		FlexTable filterFlex = new FlexTable();
		vp.add(filterFlex);
		filterFlex.setSize("567px", "39px");
	}

	public void initGrid(String un) {
		g = new Grid(1, 4);
		
		Label lb0 = new Label("N\u00E9v");
		lb0.setStyleName("Header");
		g.setWidget(0,0,lb0);
		
		Label lb1 = new Label("Felhaszn\u00E1l\u00F3on\u00E9v");
		lb1.setStyleName("Header");
		g.setWidget(0,1,lb1);
		
		Label lb2 = new Label("D\u00E1tum");
		lb2.setStyleName("Header");
		g.setWidget(0,2,lb2);
		
		Label lb3 = new Label("Darab");
		lb3.setStyleName("Header");
		g.setWidget(0,3,lb3);
		
		g.setBorderWidth(2);
		gridPanel.add(g);
		vp.add(gridPanel);
		g.setSize("489px", "34px");
		c1.getMyOrdersNumber(un, new AsyncCallback<Integer>() {

			@Override
			public void onFailure(Throwable caught) {
				Window.alert("Gond van nr");

			}

			@Override
			public void onSuccess(Integer result) {
				g.resize(result + 1, 4);
			}
		});

		c1.getMyOrders(un, new AsyncCallback<List<Order>>() {

			@Override
			public void onFailure(Throwable caught) {
				Window.alert("Gond van people");

			}

			@Override
			public void onSuccess(List<Order> result) {
				int i = 1;
				Iterator<Order> it = result.iterator();
				while (it.hasNext()) {
					Order o = (Order) it.next();

					g.setText(i, 0, o.getWho());
					g.setText(i, 1, o.getUserName());
					g.setText(i, 2, o.getDate());
					g.setText(i, 3, o.getNr() + "");
					i++;

				}

			}

		});
	}

}
